package com.fit.pages.user;

import java.util.List;

import javax.inject.Inject;

import org.apache.tapestry5.alerts.AlertManager;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.hibernate.Session;

import com.fit.entities.User;

public class BrowseUser {

	@Property
	private User user;
	
	@Inject
	private Session session;

	@InjectComponent
	private Zone zone;

	@Inject
	private AlertManager alertManager;
	
	public List<User> getUsers() {
		    return session.createCriteria(User.class).list();
	
	}

	public User findByID(Long id) {
		if (id != null) {
			return (User) session.get(User.class, id);
		}
		return null;
	}

	@CommitAfter
	Object onActionFromDelete(Long userId) {
		User u = findByID(userId);
		if (u != null) {
			alertManager.info("User " + u.getFirstName() + " "
					+ u.getLastName() + " deleted!");
			session.delete(u);
			return true;
		}
		return false;
	}
	
	
	
	
	
	
	
	
	
	
	
}
